#ifndef VARIABLE_HPP
#define VARIABLE_HPP
#include "cppbase.h"
/** 方法*/
///> 整个系统的变量API
#define _IA										mTarget.tMeasureData.usIa																				///<A相电流
#define _IB										mTarget.tMeasureData.usIb																				///<B相电流
#define _IC										mTarget.tMeasureData.usIc																				///<C相电流
#define _IN										mTarget.tMeasureData.usIn																				///<N相电流
#define _IGND									mTarget.tMeasureData.usIgnd																			///<GND电流
#define _MAX(a,b,c)					  (a > b? (a > c? (a):(c)): (b > c? (b):(c)))											///<最大
#define _MIN(a,b,c)					  (a < b? (a < c? (a):(c)): (b < c? (b):(c)))											///<最大
#define _MAX_PHASE						(_IA > _IB? (_IA > _IC? (0):(2)): (_IB > _IC? (1):(2)))					///<最大电流相
#define _MAX_CURRENT					(_IA > _IB? (_IA > _IC? (_IA):(_IC)): (_IB > _IC? (_IB):(_IC)))	///<最大电流
#define _MIN_CURRENT					(_IA < _IB? (_IA < _IC? (_IA):(_IC)): (_IB < _IC? (_IB):(_IC)))	///<最小电流
#define _AVG_CURRENT					((_IA + _IB + _IC) / 3)																					///<平均电流
#define _COMMON_ATTRIBUTE			mTarget.tCommon																									///<共同属性字
#define _PRODUCT_RATEDCURRENT	mTarget.tProductInfor.usCurrent																	///<额定电流
#define _PRODUCT_ATTRIBUTE		mTarget.tProductInfor.usAttribute																///<属性字
#define _LCD_CONFIG						mTarget.tLcdCustomConfig																				///<液晶配置
#define _SWITCH_CONFIG				mTarget.tSwitchCustomConfig																			///<拨码配置
/** 标记位*/
#define FAULT_RECORD_REASON_NONE							0
#define FAULT_RECORD_REASON_BLOCK							(1 << 6)	///<堵转故障(电机型才用)
#define FAULT_RECORD_REASON_UNBALANCE					(1 << 7)	///<相不平衡故障(电机型才用)
#define FAULT_RECORD_REASON_EARTH							(1 << 8)	///<接地故障
#define FAULT_RECORD_REASON_LONGDELAY					(1 << 9)	///<长延时/过载故障
#define FAULT_RECORD_REASON_SHORTDELAY				(1 << 10)	///<短延时故障
#define FAULT_RECORD_REASON_SHORTCIRCUIT			(1 << 11)	///<瞬动/短路故障
#define FAULT_RECORD_REASON_NCURRENT					(1 << 12)	///<N相电流故障
#define FAULT_RECORD_REASON_CCURRENT					(1 << 13)	///<L3相电流故障
#define FAULT_RECORD_REASON_BCURRENT					(1 << 14)	///<L2相电流故障
#define FAULT_RECORD_REASON_ACURRENT					(1 << 15)	///<L1相电流故障
#define FAULT_RECORD_LEN											3
/** 属性*/
#define CONFIG_ATTRIBUTE_FLAG_DISTRIB					0x0000	///<配电型
#define CONFIG_ATTRIBUTE_FLAG_MOTOR						0x0001	///<电机型
#define CONFIG_ATTRIBUTE_FLAG_LCD							0x0000	///<LCD显示
#define CONFIG_ATTRIBUTE_FLAG_SWITCH					0x0002	///<拨码型
#define CONFIG_ATTRIBUTE_FLAG_4WIRE						0x0004	///<4线制
#define CONFIG_ATTRIBUTE_FLAG_NOTRIP					0x0008	///<报警不脱扣
#define CONFIG_ATTRIBUTE_FLAG_NOARK						0x0010	///<诺雅克LOG
#define CONFIG_ATTRIBUTE_FLAG_LOGEN						0x0020	///<LOG显示使能
#define CONFIG_ATTRIBUTE_FLAG_9M2							0x0100	///<DC整流
#define CONFIG_ATTRIBUTE_FLAG_9M3							0x0200	///<DC整流
#define CONFIG_ATTRIBUTE_FLAG_9M45						0x0400	///<交流偏置电路
/** 校准参数*/
#define CONFIG_CALIBRATION_LOWER							2000	///<校准系数
#define CONFIG_CALIBRATION_UPPER							2150	///<校准系数

 /** 
 * @brief       寄存器属性
 */
const VariableAttributeTPDF ComConfig_Addr_Attribute = {2,	VARIABLE_TYPE_UINT16,	1,	{1,			247}};
const VariableAttributeTPDF ComConfig_Baud_Attribute = {2,	VARIABLE_TYPE_UINT16,	2,	{9600,	19200}};

const VariableAttributeTPDF MyProtectInfor_StructCurrent_Attribute 		= {2,		VARIABLE_TYPE_UINT16,	1,	{250,	1600}};
const VariableAttributeTPDF MyProtectInfor_Current_Attribute 					= {2,		VARIABLE_TYPE_UINT16,	2,	{32,	1600}};
const VariableAttributeTPDF MyProtectInfor_SoftDescription_Attribute 	= {16,	VARIABLE_TYPE_CHAR,		3,	{0,	255}};

const VariableAttributeTPDF ProtectInfor_ManufactoryId_Attribute 		= {1,		VARIABLE_TYPE_CHAR,		1,	{1,	255}};
const VariableAttributeTPDF ProtectInfor_Class_Attribute 						= {1,		VARIABLE_TYPE_CHAR,		2,	{4,	255}};
const VariableAttributeTPDF ProtectInfor_Serial_Attribute 					= {1,		VARIABLE_TYPE_CHAR,		3,	{3,	255}};
const VariableAttributeTPDF ProtectInfor_PriSerial_Attribute 				= {1,		VARIABLE_TYPE_CHAR,		4,	{0,	255}};
const VariableAttributeTPDF ProtectInfor_HardwareSerial_Attribute 	= {2,		VARIABLE_TYPE_UINT16,	5,	{0x0101,	65535}};
const VariableAttributeTPDF ProtectInfor_SN_Attribute 							= {24,	VARIABLE_TYPE_CHAR,		6,	{0,	255}};
const VariableAttributeTPDF ProtectInfor_Current_Attribute 					= {2,		VARIABLE_TYPE_UINT16,	7,	{32,	1600}};
const VariableAttributeTPDF ProtectInfor_StructCurrent_Attribute 		= {2,		VARIABLE_TYPE_UINT16,	8,	{250,	1600}};
const VariableAttributeTPDF ProtectInfor_Attribute_Attribute 				= {2,		VARIABLE_TYPE_UINT16,	9,	{0,	65535}};
const VariableAttributeTPDF ProtectInfor_SoftDescription_Attribute 	= {16,	VARIABLE_TYPE_CHAR,		10,	{0,	255}};

const VariableAttributeTPDF LcdCustomConfig_Motor_LongDelayTime_Attribute 				= {2,	VARIABLE_TYPE_UINT16,	2,	{5,	30}};
const VariableAttributeTPDF LcdCustomConfig_Motor_ShortDelayCurrent_Attribute 		= {2,	VARIABLE_TYPE_UINT16,	3,	{400,	1200}};
const VariableAttributeTPDF LcdCustomConfig_Motor_ShortDelayTime_Attribute 				= {2,	VARIABLE_TYPE_UINT16,	4,	{100,	100}};
const VariableAttributeTPDF LcdCustomConfig_Motor_ShortCircuitCurrent_Attribute 	= {2,	VARIABLE_TYPE_UINT16,	5,	{1500,1500}};
const VariableAttributeTPDF LcdCustomConfig_Distrib_LongDelayTime_Attribute 			= {2,	VARIABLE_TYPE_UINT16,	2,	{3,	18}};
const VariableAttributeTPDF LcdCustomConfig_Distrib_ShortDelayCurrent_Attribute 	= {2,	VARIABLE_TYPE_UINT16,	3,	{150,	1000}};
const VariableAttributeTPDF LcdCustomConfig_Distrib_ShortDelayTime_Attribute 			= {2,	VARIABLE_TYPE_UINT16,	4,	{100,	400}};
const VariableAttributeTPDF LcdCustomConfig_Distrib_ShortCircuitCurrent_Attribute = {2,	VARIABLE_TYPE_UINT16,	5,	{150,	1200}};
const VariableAttributeTPDF LcdCustomConfig_LongDelayCurrent_Attribute 				= {2,	VARIABLE_TYPE_UINT16,	1,	{40,	100}};
const VariableAttributeTPDF LcdCustomConfig_EarthCurrent_Attribute 					= {2,	VARIABLE_TYPE_UINT16,	6,	{40,	100}};
const VariableAttributeTPDF LcdCustomConfig_EarthTime_Attribute 					= {2,	VARIABLE_TYPE_UINT16,	7,	{100,	400}};
const VariableAttributeTPDF LcdCustomConfig_NCurrent_Attribute 						= {2,	VARIABLE_TYPE_UINT16,	8,	{5,	10}};
const VariableAttributeTPDF LcdCustomConfig_UnbalanceRated_Attribute 				= {2,	VARIABLE_TYPE_UINT16,	9,	{30,	90}};
const VariableAttributeTPDF LcdCustomConfig_UnbalanceTime_Attribute 				= {2,	VARIABLE_TYPE_UINT16,	10,	{4,	10}};
const VariableAttributeTPDF LcdCustomConfig_BlockCurrent_Attribute 					= {2,	VARIABLE_TYPE_UINT16,	11,	{300,	1000}};
const VariableAttributeTPDF LcdCustomConfig_BlockTime_Attribute 					= {2,	VARIABLE_TYPE_UINT16,	12,	{1,	30}};
const VariableAttributeTPDF LcdCustomConfig_LongDelayEnable_Attribute 			    = {2,	VARIABLE_TYPE_UINT16,	13,	{0,	1}};
const VariableAttributeTPDF LcdCustomConfig_ShortDelayEnable_Attribute 						= {2,	VARIABLE_TYPE_UINT16,	14,	{0,	1}};
const VariableAttributeTPDF LcdCustomConfig_ShortCircuitEnable_Attribute 					= {2,	VARIABLE_TYPE_UINT16,	15,	{0,	1}};
const VariableAttributeTPDF LcdCustomConfig_NEnable_Attribute 										= {2,	VARIABLE_TYPE_UINT16,	16,	{0,	1}};
const VariableAttributeTPDF LcdCustomConfig_EarthEnable_Attribute 								= {2,	VARIABLE_TYPE_UINT16,	17,	{0,	1}};
const VariableAttributeTPDF LcdCustomConfig_UnbalanceEnable_Attribute 						= {2,	VARIABLE_TYPE_UINT16,	18,	{0,	1}};
const VariableAttributeTPDF LcdCustomConfig_BlockEnable_Attribute 								= {2,	VARIABLE_TYPE_UINT16,	19,	{0,	1}};
const VariableAttributeTPDF LcdCustomConfig_Frequnce_Attribute 						= {2,	VARIABLE_TYPE_UINT16,	20,	{0,	1}};
 
const VariableAttributeTPDF Security_NetChangeEnable_Attribute 	= {2,	VARIABLE_TYPE_UINT16,	1,	{0,	1}};
const VariableAttributeTPDF Security_NetControlEnable_Attribute = {2,	VARIABLE_TYPE_UINT16,	2,	{0,	1}};
const VariableAttributeTPDF Security_Language_Attribute 				= {2,	VARIABLE_TYPE_UINT16,	3,	{0,	1}};
const VariableAttributeTPDF Security_PasswordEnable_Attribute 	= {2,	VARIABLE_TYPE_UINT16,	4,	{0,	1}};
const VariableAttributeTPDF Security_Password_Attribute 				= {8,	VARIABLE_TYPE_CHAR,		5,	{0,	255}};

 /** 
 * @brief       分区表地址定义
 */
#define MODBUS_ADDR_MEASUREDATA														0x0001
#define MODBUS_ADDR_FAULTRECORD														0x003b
#define MODBUS_ADDR_COMCONFIG															0x0100
#define MODBUS_ADDR_MYPRODUCTINFOR												0x0180
#define MODBUS_ADDR_CUSTOMCONFIG													0x2007
#define MODBUS_ADDR_CONTROLCOMMAND												0x2800

typedef enum
{
	WRITE_COMMAND_COMCONFIG = 0,		///<通讯配置
	WRITE_COMMAND_CUSTOMCONFIG,			///<用户配置
	WRITE_COMMAND_SECURITY,					///<其他/安全配置
	WRITE_COMMAND_PRODUCTINFOR,			///<工厂配置
	WRITE_COMMAND_RESET,						///<复位
	WRITE_COMMAND_CALIBRATION,			///<校准
	WRITE_COMMAND_CONTROL,					///<控制
}WriteCommandTypeTPDF;						///<写指令类型

typedef struct
{
	uint8_t ucData[24];							///<数据
	uint16_t usLen;									///<数据长度
	uint16_t usOffset;							///<源数据偏移量
	uint32_t uiKey;									///<键值
}WriteDataTPDF;										///<写数据定义

typedef struct
{
	WriteCommandTypeTPDF tType;			///<类型
	WriteDataTPDF tWriteData[20];		///<写数据
	uint16_t usLen;									///<写数据长度
}WriteCommandTPDF;								///<写指令

typedef struct
{
	bool bResult;										///<操作结果
}WriteStatusTPDF;									///<写指令状态

typedef struct
{
	uint16_t usIa;									///<A相电流
	uint16_t usIb;									///<B相电流
	uint16_t usIc;									///<C相电流
	uint16_t usIn;									///<N相电流
	uint16_t usIgnd;								///<GND电流
}MeasureDataPDF;									///<测量

typedef struct
{
	uint16_t usReason;							///<故障原因
	uint16_t usCurrent;							///<故障相电流
}FaultRecordTPDF;									///<故障记录

typedef struct
{
	uint16_t usAddr;								///<通讯地址
	uint16_t usBaud;								///<波特率
}ComConfigTPDF;										///<通讯配置

typedef struct
{
	uint16_t usStructCurrent;				///<壳架电流
	uint16_t usCurrent;							///设备电流规格
	uint8_t ucSoftDescription[16];	///<自定义软件描述
}MyProductInforTPDF;							///<设备信息

typedef struct
{
	uint8_t ucManufactoryId;				///<厂家ID
	uint8_t ucClass;								///<产品分类
	uint8_t ucSerial;								///<产品系列
	uint8_t ucPriSerial;						///<产品子系列
	uint8_t ucHardwareSerial[2];		///<硬件版本号
	uint8_t ucSN[24];								///SN码
	uint16_t usCurrent;							///设备电流规格
	uint16_t usStructCurrent;				///<壳架电流
	uint16_t usAttribute;						///<属性
	uint8_t ucSoftDescription[16];	///<自定义软件描述
}ProductInforTPDF;								///<设备信息

typedef struct
{
	uint16_t usLongDelayCurrent;		///<长延时电流整定值
	uint16_t usLongDelayTime;				///<长延时时间整定值
	uint16_t usShortDelayCurrent;		///<短延时电流整定值
	uint16_t usShortDelayTime;			///<短延时时间整定值
	uint16_t usShortCircuitCurrent;	///<短路瞬时电流整定值
	uint16_t usEarthCurrent;				///<接地保护电流整定值(液晶)
	uint16_t usEarthTime;						///<接地保护延时时间整定值(液晶)
	uint16_t usNCurrent;						///<N相保护电流整定值
	uint16_t usUnbalanceRated;			///<三相不平衡率
	uint16_t usUnbalanceTime;				///<三相不平衡延时时间整定值
	uint16_t usBlockCurrent;				///<堵转电流整定值(液晶)
	uint16_t usBlockTime;						///<堵转保护延时时间(液晶)
	uint16_t usLongDelayEnable;			///<长延时功能使能(液晶)
	uint16_t usShortDelayEnable;		///<短延时功能使能(液晶)
	uint16_t usShortCircuitEnable;	///<瞬时功能使能(液晶)
	uint16_t usNEnable;							///<N相保护使能
	uint16_t usEarthEnable;					///<接地功能使能(液晶)
	uint16_t usUnbalanceEnable;			///<三相不平功能使能(液晶)
	uint16_t usBlockEnable;					///<堵转功能使能(液晶)
	uint16_t usFrequnce;						///<电流频率
}LcdCustomConfigTPDF;							///<用户配置(保护配置,液晶型)

typedef struct
{
	uint16_t usLongDelayCurrent;		///<长延时电流整定值
	uint16_t usLongDelayTime;				///<长延时时间整定值
	uint16_t usShortDelayCurrent;		///<短延时电流整定值
	uint16_t usShortDelayTime;			///<短延时时间整定值
	uint16_t usShortCircuitCurrent;	///<短路瞬时电流整定值
	uint16_t usReserver[2];					///<保留
	uint16_t usNCurrent;						///<N相保护电流整定值
	uint16_t usUnbalanceRated;			///<三相不平衡率
	uint16_t usUnbalanceTime;				///<三相不平衡延时时间整定值
	uint16_t usReserver2[10];				///<保留
}SwitchCustomConfigTPDF;					///<用户配置(保护配置,拨码型)

typedef struct
{
	uint16_t usCommand;							///<控制命令
}ControlCommandTPDF;							///<控制命令

typedef struct
{
	uint16_t usNetChangeEnable;			///<网络修改允许
	uint16_t usNetControlEnable;		///<网络控制允许
	uint16_t usLanguage;						///<语言
	uint16_t usPasswordEnable;			///<口令保护
	uint8_t ucPassword[8];					///<口令	
}SecurityConfigTPDF;							///<其他/安全配置

typedef struct
{
	uint32_t uiIr;									///<长延时电流
	uint32_t uiLongDelayCurrent;		///<长延时电流整定值
	uint32_t uiLongDelayTime;				///<长延时时间整定值
	uint32_t uiShortDelayCurrent;		///<短延时电流整定值
	uint32_t uiShortDelayTime;			///<短延时时间整定值
	uint32_t uiShortCircuitCurrent;	///<短路瞬时电流整定值
	uint32_t uiEarthCurrent;				///<接地保护电流整定值
	uint32_t uiEarthTime;						///<接地保护延时时间整定值
	uint32_t uiNLongDelayCurrent;		///<N相长延时电流整定值
	uint32_t uiNLongDelayTime;			///<N相长延时时间整定值
	uint32_t uiNShortDelayCurrent;	///<N相短延时电流整定值
	uint32_t uiNShortDelayTime;			///<N相短延时时间整定值
	uint32_t uiNShortCircuitCurrent;///<短路瞬时电流整定值
	uint32_t uiUnbalanceRated;			///<三相不平衡率
	uint32_t uiUnbalanceTime;				///<三相不平衡延时时间整定值
	uint32_t uiBlockCurrent;				///<堵转电流整定值
	uint32_t uiBlockTime;						///<堵转保护延时时间
	bool bLongDelayEnable;					///<长延时功能使能
	bool bShortDelayEnable;					///<短延时功能使能
	bool bShortCircuitEnable;				///<瞬时功能使能
	bool bNLongDelayEnable;					///<N相长延时功能使能
	bool bNShortDelayEnable;				///<N相短延时功能使能
	bool bNShortCircuitEnable;			///<N相瞬时功能使能
	bool bEarthEnable;							///<接地功能使能
	bool bUnbalanceEnable;					///<三相不平功能使能
	bool bBlockEnable;							///<堵转功能使能
}CommonAttributeTPDF;							///<共同属性

typedef struct
{
	MeasureDataPDF tMeasureData;
	FaultRecordTPDF tFaultRecord[FAULT_RECORD_LEN];
	ComConfigTPDF tComConfig;
	MyProductInforTPDF tMyProductInfor;
	ProductInforTPDF tProductInfor;
	LcdCustomConfigTPDF tLcdCustomConfig;
	SwitchCustomConfigTPDF tSwitchCustomConfig;
	SecurityConfigTPDF tSecurityConfig;
	CommonAttributeTPDF tCommon;
}TargetTPDF;

extern TargetTPDF mTarget;
#endif
